package com.shenqi.parking.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shenqi.parking.mapper.ParkingSpotsMapper;
import com.shenqi.parking.domain.ParkingSpots;
import com.shenqi.parking.service.IParkingSpotsService;

/**
 * 车位出租Service业务层处理
 * 
 * @author shenqi
 * @date 2025-04-07
 */
@Service
public class ParkingSpotsServiceImpl implements IParkingSpotsService 
{
    @Autowired
    private ParkingSpotsMapper parkingSpotsMapper;

    /**
     * 查询车位出租
     * 
     * @param id 车位出租主键
     * @return 车位出租
     */
    @Override
    public ParkingSpots selectParkingSpotsById(Long id)
    {
        return parkingSpotsMapper.selectParkingSpotsById(id);
    }

    /**
     * 查询车位出租列表
     * 
     * @param parkingSpots 车位出租
     * @return 车位出租
     */
    @Override
    public List<ParkingSpots> selectParkingSpotsList(ParkingSpots parkingSpots)
    {
        return parkingSpotsMapper.selectParkingSpotsList(parkingSpots);
    }

    /**
     * 新增车位出租
     * 
     * @param parkingSpots 车位出租
     * @return 结果
     */
    @Override
    public int insertParkingSpots(ParkingSpots parkingSpots)
    {
        parkingSpots.setCreateTime(DateUtils.getNowDate());
        return parkingSpotsMapper.insertParkingSpots(parkingSpots);
    }

    /**
     * 修改车位出租
     * 
     * @param parkingSpots 车位出租
     * @return 结果
     */
    @Override
    public int updateParkingSpots(ParkingSpots parkingSpots)
    {
        parkingSpots.setUpdateTime(DateUtils.getNowDate());
        return parkingSpotsMapper.updateParkingSpots(parkingSpots);
    }

    /**
     * 批量删除车位出租
     * 
     * @param ids 需要删除的车位出租主键
     * @return 结果
     */
    @Override
    public int deleteParkingSpotsByIds(Long[] ids)
    {
        return parkingSpotsMapper.deleteParkingSpotsByIds(ids);
    }

    /**
     * 删除车位出租信息
     * 
     * @param id 车位出租主键
     * @return 结果
     */
    @Override
    public int deleteParkingSpotsById(Long id)
    {
        return parkingSpotsMapper.deleteParkingSpotsById(id);
    }

    @Override
    public int deleteMyParkingSpotsByIds(Long[] ids) {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
            int rows = parkingSpotsMapper.deleteParkingSpotsByIds(ids);
            return rows;
        }
        for (Long id: ids) {
            boolean exist = parkingSpotsMapper.existsByIdAndOwner(id, SecurityUtils.getUsername());
            if(!exist) {
                throw new ServiceException("不存在相关车位信息");
            }
        }
        int rows = parkingSpotsMapper.deleteParkingSpotsByIds(ids);
        return rows;
    }
}
